package com.xu.chuang.show.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xu.chuang.show.R;
import com.xu.chuang.show.bean.ShowUser;
import com.xu.chuang.show.network.SyncRequest;
import com.xu.chuang.show.service.impl.UserReqImpl;
import com.xu.chuang.show.service.inte.UserReqService;

import java.util.Random;

public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        SyncRequest.syncReq();  //设置同步网络请求

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.register);
        setSupportActionBar(toolbar);
        Button commitButton = (Button) findViewById(R.id.button__register_commit);
        commitButton.setOnClickListener(commitClick);
    }

    private Button.OnClickListener commitClick = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(commitAble()) {
                Intent intent = new Intent(v.getContext(), LoginActivity.class);
                startActivity(intent);
            }
        }
    };

    private boolean commitAble() {
        EditText mobileView = (EditText) findViewById(R.id.mobile);
        AutoCompleteTextView mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
        EditText mPasswordView = (EditText) findViewById(R.id.password);
        String mobile = mobileView.getText().toString();
        String email = mEmailView.getText().toString();
        String password = mPasswordView.getText().toString();

        boolean cancel = false;
        View focusView = null;

        if (TextUtils.isEmpty(mobile)) {
            mobileView.setError(getString(R.string.error_field_required));
            focusView = mobileView;
            cancel = true;
        } else if (!isMobileValid(mobile)) {
            mobileView.setError(getString(R.string.error_field_invalid));
            focusView = mobileView;
            cancel = true;
        } else if (TextUtils.isEmpty(email)) {
            mEmailView.setError(getString(R.string.error_field_required));
            focusView = mEmailView;
            cancel = true;
        } else if (!isEmailValid(email)) {
            mEmailView.setError(getString(R.string.error_field_invalid));
            focusView = mEmailView;
            cancel = true;
        } else if (TextUtils.isEmpty(password)) {
            mPasswordView.setError(getString(R.string.error_field_required));
            focusView = mPasswordView;
            cancel = true;
        } else if (!isPasswordValid(password)) {
            mPasswordView.setError(getString(R.string.error_field_invalid));
            focusView = mPasswordView;
            cancel = true;
        }

        if (cancel) {
            focusView.requestFocus();
        } else {

            UserReqService userReqService = new UserReqImpl();
            if(userReqService.checkedRepeat(email) != null){
                cancel = true;
                Toast.makeText(this,getString(R.string.already_register), Toast.LENGTH_SHORT).show();
            } else {
                ShowUser showUser = new ShowUser();
                showUser.setEmail(email);
                showUser.setMobile(mobile);
                showUser.setPassword(password);
                showUser.setLevel(1L);
                showUser.setScope(1000D);
                Integer num = (int) (Math.random()*10);
                showUser.setIconUrl(num.toString());
                if( num<=3) {
                    showUser.setNickName(getString(R.string.magic_rain));
                } else if (num<=6) {
                    showUser.setNickName(getString(R.string.wu_peng));
                } else if (num<=8){
                    showUser.setNickName(getString(R.string.gao_min));
                } else{
                    showUser.setNickName(getString(R.string.fan));
                }

                userReqService.register(showUser);
            }

        }
        return !cancel;
    }

    private boolean isMobileValid(String mobileString) {
        return mobileString.length() == 11;
    }
    private boolean isEmailValid(String emailString) {
        return emailString.contains("@");
    }

    private boolean isPasswordValid(String pwdString) {
        return pwdString.length() >= 6 && pwdString.length() <= 18;
    }
}
